package com.example.pagingdemo;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.paging.PagedList;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.example.pagingdemo.databinding.ActivityPagingBinding;
import com.example.pagingdemo.model.User;
import com.example.pagingdemo.paging.user.UserPageListAdapter;
import com.example.pagingdemo.paging.user.UserViewModel;

/**
 * @author: tuyrt7 on 2020/9/9.
 */
public class PageKeyActivity extends AppCompatActivity {

    private ActivityPagingBinding mBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_paging);

        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mBinding.recyclerView.setHasFixedSize(true);
        UserPageListAdapter adapter = new UserPageListAdapter();
        mBinding.recyclerView.setAdapter(adapter);
        UserViewModel viewModel = new ViewModelProvider(this).get(UserViewModel.class);
        viewModel.getPagedData().observe(this, new Observer<PagedList<User>>() {
            @Override
            public void onChanged(PagedList<User> users) {
                adapter.submitList(users);
            }
        });
    }
}
